from llama_index.core.base.llms.types import ChatMessage
from llama_index.llms.openai import OpenAI

llm=OpenAI(model="gpt-4.1-nano",
                   api_key="sk-CftUbVSsA61lwwgMz9xvt6znTunQZfgBP8ZCVLbQsKfXUR6k",
                   api_base="https://www.henapi.top/v1",
                   )
# response = llm.complete("William Shakespeare is ")
# print(response)


handle = llm.stream_complete("William Shakespeare is ")

for token in handle:
    print(token.delta, end="", flush=True)


messages = [
    ChatMessage(role="system", content="You are a helpful assistant."),
    ChatMessage(role="user", content="Tell me a joke."),
]
chat_response = llm.chat(messages)

print(chat_response)